//
//  GXJViewController.m
//  Singapore_powerbank
//
//  Created by 麦鱼科技 on 2017/7/20.
//  Copyright © 2017年 麦鱼科技. All rights reserved.
//

#import "GXJViewController.h"

@interface GXJViewController ()

@end

@implementation GXJViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = UIColorWhite;
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.extendedLayoutIncludesOpaqueBars = NO;
    self.modalPresentationCapturesStatusBarAppearance = NO;
    [self.navigationController.navigationBar setHidden:YES];
    //透明度
//    self.navigationController.navigationBar.translucent = NO;
//    UIBarButtonItem *back = [[UIBarButtonItem alloc]initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];
//    self.navigationItem.backBarButtonItem = back;
    //去线条
//    [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
//    [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
    [self createUI];
}

- (void)dealloc {
    NSLog(@"---%@---销毁了",NSStringFromClass([self class]));
}

- (void)createUI{
    
}

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    if (self.navigationController) {
        self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    }
}

- (void)showBackImage{
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    imageView.image = [UIImage imageNamed:@"(矩形)750-1334"];
    [self.view insertSubview:imageView atIndex:0];
}

- (void)initTitle:(NSString *)title tintColor:(UIColor *)tintColor HaveBack:(BOOL)isBack{
    self.titleView = [[UIView alloc] initWithFrame:CGRectMake(0,0,kScreenWidth,kSafeAreaTopHeight)];
    [self.view addSubview:self.titleView];
    self.titleLab = [[UILabel alloc] initWithFrame:CGRectMake(0,0,180,30)];
    self.titleLab.mj_w = 220;
    self.titleLab.center = CGPointMake(kScreenWidth / 2.0,22.0 + KStatusBarHeight);
    self.titleLab.backgroundColor = [UIColor clearColor];
    self.titleLab.textAlignment = NSTextAlignmentCenter;
    self.titleLab.textColor = tintColor;
    self.titleLab.font = [UIFont br_fontM:21];
    self.titleLab.text = title;
    [self.titleView addSubview:self.titleLab];
    [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.titleView).offset(60);
        make.bottom.equalTo(self.titleView);
        make.height.mas_equalTo(44);
        make.right.equalTo(self.titleView.mas_right).offset(-60);
    }];

//    if (isBack) {
//        if (CGColorEqualToColor(tintColor.CGColor,  UIColor.qd_titleTextColor.CGColor)) {
            [self createBackButton];
//        }else {
//            [self createWhiteButton];
//        }
//
//    }
}

- (void)initWhiteBarWithTitle:(NSString *)title isBack:(BOOL)isBack{
    [self initTitle:title tintColor:UIColor.whiteColor HaveBack:isBack];
    self.titleView.backgroundColor = kColorMain;
}

- (void)initBlueBarWithTitle:(NSString *)title isBack:(BOOL)isBack{
    [self initTitle:title tintColor:[UIColor whiteColor] HaveBack:isBack];
    self.titleView.backgroundColor = [UIColor jk_colorWithHexString:@"#00B9FF"];
}

- (void)createBackButton {
    CGRect statusRect = kStatusRect;
    self.backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.backBtn setImage:[UIImage imageNamed:@"arrow_left_white44"] forState:UIControlStateNormal];
    [self.backBtn setImage:[UIImage imageNamed:@"arrow_left_white44"] forState:UIControlStateHighlighted];
    [self.backBtn sizeToFit];
    self.backBtn.frame = CGRectMake(0,statusRect.size.height, 60, 44);
    self.backBtn.imageEdgeInsets = UIEdgeInsetsMake(0,15,0,0);
    self.backBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [self.backBtn addTarget:self action:@selector(handleBackAction) forControlEvents:UIControlEventTouchUpInside];
    [self.titleView addSubview:self.backBtn];
}

- (void)createWhiteButton {
    CGRect statusRect = kStatusRect;
    self.backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.backBtn setImage:[UIImage imageNamed:@"arrow_left_gary44"] forState:UIControlStateNormal];
    [self.backBtn setImage:[UIImage imageNamed:@"arrow_left_gary44"] forState:UIControlStateHighlighted];
    [self.backBtn sizeToFit];
    self.backBtn.frame = CGRectMake(0,statusRect.size.height, 60, 44);
    self.backBtn.imageEdgeInsets = UIEdgeInsetsMake(0,10,0,0);
    self.backBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [self.backBtn addTarget:self action:@selector(handleBackAction) forControlEvents:UIControlEventTouchUpInside];
    [self.titleView addSubview:self.backBtn];
}

- (void)handleBackAction {
    if (self.navigationController.viewControllers.count > 0) {
        [self.navigationController popViewControllerAnimated:YES];
    }else {
        [self dismissViewControllerAnimated:YES completion:nil];
    }
    
}

- (UIButton *)addLeftButtonWithIcon:(UIImage *)iconImg action:(SEL)action {
    UIButton *aBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    aBtn.frame = CGRectMake(0,KStatusBarHeight,70,44);
    aBtn.backgroundColor = [UIColor clearColor];
    [aBtn addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
    if (iconImg) {
        [aBtn setImage:iconImg forState:UIControlStateNormal];
        [aBtn setImage:iconImg forState:UIControlStateHighlighted];
    }
    aBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    aBtn.imageEdgeInsets = UIEdgeInsetsMake(0,24,0,0);
    [self.titleView addSubview:aBtn];
    return aBtn;
}

- (UIButton *)addRightButtonWithIcon:(UIImage *)iconImg action:(SEL)action {
    UIButton *aBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    aBtn.frame = CGRectMake(self.titleView.mj_w - 70,KStatusBarHeight,70,44);
    aBtn.backgroundColor = [UIColor clearColor];
    [aBtn addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
    if (iconImg) {
        [aBtn setImage:iconImg forState:UIControlStateNormal];
        [aBtn setImage:iconImg forState:UIControlStateHighlighted];
    }
    aBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    aBtn.imageEdgeInsets = UIEdgeInsetsMake(0,0,0,24);
    [self.titleView addSubview:aBtn];
    return aBtn;
}

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 44 + KStatusBarHeight, kScreenWidth, kScreenHeight - 44 - KStatusBarHeight) style:UITableViewStylePlain];
        _tableView.tableFooterView = [UIView new];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
    }
    return _tableView;
}

#pragma mark - 下拉刷新、加载更多
- (MJRefreshStateHeader *)addRefreshActionForView:(UIScrollView *)aView {
    // 设置回调（一旦进入刷新状态，就调用target的action，也就是调用self的loadNewData方法）
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(handleListRefreshAction)];
    // 设置自动切换透明度(在导航栏下面自动隐藏)
    header.automaticallyChangeAlpha = YES;
    // 隐藏时间
    header.lastUpdatedTimeLabel.hidden = YES;
    aView.mj_header = header;
    
    return header;
}

- (MJRefreshFooter *)addLoadMoreActionForView:(UIScrollView *)aView {
    // 设置回调（一旦进入刷新状态，就调用target的action，也就是调用self的loadMoreData方法）
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(handleListLoadMoreAction)];
    footer.automaticallyHidden = YES;
    aView.mj_footer = footer;
    return footer;
}

- (void)handleListRefreshAction {
    
}

- (void)handleListLoadMoreAction {
    
}

- (void)handleEndRefreshForView:(UIScrollView *)aView {
    [aView.mj_header endRefreshing];
    [aView.mj_footer endRefreshing];
}

- (void)handleEndRefreshForViewWithNoMoreData:(UIScrollView *)aView
{
    [aView.mj_footer endRefreshingWithNoMoreData];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (BOOL)checkLogionState
{
    if ([BRUserManager share].isLogin) {
        return YES;
    }else{
        return NO;
    }
}

- (void)presentLoginView
{
//    if ([[self getCurrentVC] isKindOfClass: [LoginViewController class]]) {
//        return;
//    }
//    LoginViewController *loginView = [[LoginViewController alloc] init];
//    loginView.modalPresentationStyle = UIModalPresentationFullScreen;
//    [self presentViewController: loginView animated: YES completion: ^{
//        [self.navigationController popViewControllerAnimated: nil];
//    }];
}

- (UIViewController *)getCurrentVC
{
    UIViewController *rootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
    
    UIViewController *currentVC = [self getCurrentVCFrom:rootViewController];
    
    return currentVC;
}

- (UIViewController *)getCurrentVCFrom:(UIViewController *)rootVC
{
    UIViewController *currentVC;
    
    if ([rootVC presentedViewController]) {
        // 视图是被presented出来的
        
        rootVC = [rootVC presentedViewController];
    }
    
    if ([rootVC isKindOfClass:[UITabBarController class]]) {
        // 根视图为UITabBarController
        
        currentVC = [self getCurrentVCFrom:[(UITabBarController *)rootVC selectedViewController]];
        
    } else if ([rootVC isKindOfClass:[UINavigationController class]]){
        // 根视图为UINavigationController
        
        currentVC = [self getCurrentVCFrom:[(UINavigationController *)rootVC visibleViewController]];
        
    } else {
        // 根视图为非导航类
        
        currentVC = rootVC;
    }
    
    return currentVC;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
